package slice

import "fmt"

func Delete[T any](sli []T, index int) ([]T, error) {
	sliLen := len(sli)
	if index < 0 || index >= sliLen {
		return nil, fmt.Errorf("索引必须大于等于0且小于%d", sliLen)
	}

	// for i := index; i < sliLen-1; i++ {
	// 	sli[i] = sli[i+1]
	// }

	for k, v := range sli[index : sliLen-1] {
		sli[k+1] = v
	}

	return sli[:sliLen-1], nil
}

func DeleteByAppend[T any](sli []T, index int) ([]T, error) {
	sliLen := len(sli)
	if index < 0 || index >= sliLen {
		return nil, fmt.Errorf("索引必须大于等于0且小于%d", sliLen)
	}

	return append(sli[:index], sli[index+1:]...), nil
}
